package com.zerous.staria.ui;

import com.google.gson.*;
import com.zerous.framework.*;
import com.zerous.framework.ui.*;
import com.zerous.staria.ui.*;
import java.util.*;

public class Layer
{
	private Map<String, UIBase> gui;
	String uiKinds[] = {"Button", "Rect", "CheckBox"};
	public Layer(String layout)
	{
		gui = new HashMap<String, UIBase>();	
		Gson g = new GsonBuilder().create();
		Layout l = g.fromJson(layout, Layout.class);
		for(UIAttrib attr : l.guis)
		{
			UIBase ui = null;
			String name = attr.name;

			if(attr.kind.equals(uiKinds[1]))
			{
				//好长@_@
				ui = new Rectangle(new Point(Integer.valueOf(attr.x), Integer.valueOf(attr.y)), new Point(Integer.valueOf(attr.w), Integer.valueOf(attr.h)));
			} 
			else if(attr.kind.equals(uiKinds[0]))
			{
				//ui = new Button(attr.x, attr.y, attr.w, attr.h);
			}
			else if(attr.kind.equals(""))
			{
				
			}
			gui.put(name, ui);
		}
	}

	public void update()
	{
		//t.update();
		Iterator i = gui.entrySet().iterator();
		while(i.hasNext())
		{
			((UIBase)((Map.Entry)i.next()).getValue()).update();
		}
	}

	//@Override
	public void render()
	{
		//Zex.graphics.clear(0xffffffff);
		//t.draw();
		Iterator i = gui.entrySet().iterator();
		while(i.hasNext())
		{
			((UIBase)((Map.Entry)i.next()).getValue()).draw();
		}
	}

	protected UIBase getUI(String name)
	{
		return gui.get(name);
	}
}
